package jmine.tec.web.servlet.filter.transaction;

import javax.servlet.ServletRequest;

import org.springframework.transaction.TransactionDefinition;

import bancosys.tec.persist.authorization.AuthorizationContext;
import bancosys.tec.persist.spring.transaction.PartitionTransactionDefinition;

/**
 * Factory de TransactionAttribute que leva em consideração parâmetros de base quente/fria presentes no contexto.
 * 
 * @author lundberg
 */
public class PartitionTransactionAttributeFactory implements TransactionDefinitionFactory {

    public static final String PARTITION_FIELD = "jmine.tec.web.servlet.filter.transactionPartition";

    public static final String COLD = "cold";

    public static final String HOT = "hot";

    /**
     * {@inheritDoc}
     */
    public TransactionDefinition create(ServletRequest request) {
        PartitionTransactionDefinition def = new PartitionTransactionDefinition();
        String partition = request.getParameter(PARTITION_FIELD);
        if (COLD.equals(partition)) {
            def.setPartition(AuthorizationContext.cold);
        } else {
            def.setPartition(AuthorizationContext.hot);
        }
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        return def;
    }

}
